//
// Copyright (c) 2009 All Right Reserved
//
// vl
//
// 2009-01-01
// Contains ...
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using LargoCommon.Localization;
namespace LargoCommon.Music
{
///
/// Melodic Item Group.
///
public class MelodicItemGroup {
#region Fields
///
/// Melodic Items.
///
private List items;
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The given items.
/// The given line.
public MelodicItemGroup(IList givenItems, MusicalLine givenLine) {
Contract.Requires(givenItems != null);
this.Items = givenItems as List;
this.Length = givenItems.Count;
var rs = new StringBuilder();
var ms = new StringBuilder();
foreach (var item in givenItems) {
if (item.RhythmicStructure != null) {
rs.Append(item.RhythmicStructure.GetStructuralCode);
}
rs.Append(";");
if (item.MelodicStructure != null) {
ms.Append(item.MelodicStructure.GetStructuralCode);
}
ms.Append(";");
}
this.RhythmicIdentifier = rs.ToString(); //// Identifier of the rhythmic motive
this.MelodicIdentifier = ms.ToString(); //// Identifier of the melodic motive
this.MusicalLine = givenLine;
if (this.Items == null) {
return;
}
var firstItem = this.Items.First();
this.FirstBarNumber = firstItem.MusicalBar.BarNumber;
}
#endregion
#region Properties
///
/// Gets the length.
///
///
/// The length.
///
public int Length { get; }
///
/// Gets the rhythmic identifier.
///
///
/// The rhythmic identifier.
///
public string RhythmicIdentifier { get; }
///
/// Gets the melodic identifier.
///
///
/// The melodic identifier.
///
public string MelodicIdentifier { get; }
///
/// Gets or sets the musical track.
///
///
/// The musical track.
///
public MusicalLine MusicalLine { get; set; }
///
/// Gets the first bar number.
///
///
/// The first bar number.
///
// ReSharper disable once UnusedAutoPropertyAccessor.Local
private int FirstBarNumber { get; }
///
/// Gets or sets the items.
///
///
/// The items.
///
private List Items {
get {
Contract.Ensures(Contract.Result>() != null);
if (this.items == null) {
throw new InvalidOperationException("No melodic items.");
}
return this.items;
}
set => this.items = value ?? throw new ArgumentException(LocalizedMusic.String("Argument cannot be null."), nameof(value));
}
#endregion
#region Public static methods
#endregion
#region Methods
///
/// Rhythmic change.
///
///
/// Returns value.
///
public MusicalArea GetArea() {
var firstItem = this.Items.First();
var length = this.Length;
var p0 = MusicalPoint.GetPoint(this.MusicalLine.LineIndex, firstItem.MusicalBar.BarNumber);
var p1 = MusicalPoint.GetPoint(this.MusicalLine.LineIndex, firstItem.MusicalBar.BarNumber + length);
var area = new MusicalArea(p0, p1);
return area;
}
///
/// Rhythmic motive.
///
/// The given number.
/// Name of the given.
///
/// Returns value.
///
public RhythmicMotive RhythmicMotive(int givenNumber, string givenName) {
var firstItem = this.Items.First();
var rhythmicStructures = new RhythmicStructureCollection();
foreach (var item in this.Items) {
if (item.RhythmicStructure == null) {
continue;
}
rhythmicStructures.Add(item.RhythmicStructure);
}
var rhythmicMotive = new RhythmicMotive(givenName, rhythmicStructures) {
FirstBarNumber = firstItem.MusicalBar.BarNumber,
Number = givenNumber
};
return rhythmicMotive;
}
///
/// Melodic motive.
///
/// The given number.
/// Name of the given.
/// Returns value.
public MelodicMotive MelodicMotive(int givenNumber, string givenName) {
var firstItem = this.Items.First();
var melodicStructures = new MelodicStructureCollection();
foreach (var item in this.Items) {
if (item.MelodicStructure == null) {
continue;
}
melodicStructures.Add(item.MelodicStructure);
}
var melodicMotive = new MelodicMotive(givenName, melodicStructures) {
FirstBarNumber = firstItem.MusicalBar.BarNumber,
Number = givenNumber,
MelodicFunction = MusicalToneCollection.GuessMelodicType(this.MusicalLine.FirstStatus.BandType, true),
Octave = this.MusicalLine.FirstStatus.Octave
};
return melodicMotive;
}
#endregion
#region String representation
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString() {
var sb = new StringBuilder();
foreach (var item in this.Items) {
sb.Append(item);
sb.Append("| ");
}
return sb.ToString();
}
#endregion
}
}